package com.shouhuobao.bhi.collect;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.logic.AppLogic;
import com.shouhuobao.bhi.collect.model.CollectParcelBean;

import droid.frame.utils.android.MathUtils;

/**
 * 网点待取包裹
 * 
 * @author wind
 * 
 */
public class CollectParcelCheckListAdapter extends BaseAdapter {

	private ArrayList<CollectParcelBean> mList;
	private LayoutInflater mInflater;
	private SimpleDateFormat mSimpleDateFormat;

	public CollectParcelCheckListAdapter(Context context, ArrayList<CollectParcelBean> list) {
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mList = list;
		mSimpleDateFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		//
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.collect_agent_point_parcel_check_item, parent, false);
			viewHolder.mParcelOriginPic = (ImageView) convertView.findViewById(R.id.parcel_origin_pic);
			viewHolder.mParcelOriginCompany = (TextView) convertView.findViewById(R.id.parcel_origin_company);
			viewHolder.mParcelNumber = (TextView) convertView.findViewById(R.id.parcel_number);
			viewHolder.mParcelArriveShopTime = (TextView) convertView.findViewById(R.id.parcel_arrive_shop_time);
			viewHolder.mParcelNeedPayMoney = (TextView) convertView.findViewById(R.id.parcel_need_pay_money);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.mParcelOriginCompany.setText(mList.get(position).getExpressCom());
		viewHolder.mParcelNumber.setText(mList.get(position).getMailNo());
		// if(mList.get(position).getReceiveDate()!=null){
		// viewHolder.mParcelArriveShopTime.setText(mSimpleDateFormat.format(new Date(Long.parseLong(mList.get(position).getReceiveDate()))));
		// }
		if (mList.get(position).getReceiveSeq() != null) {
			viewHolder.mParcelArriveShopTime.setText("包裹编码 " + mList.get(position).getReceiveSeq());

		}
		// 加载包裹公司图标
		String picUrl = mList.get(position).getImageUrl();
		if (!TextUtils.isEmpty(picUrl)) {
			AppLogic.getInstance().loadImage(picUrl, viewHolder.mParcelOriginPic);
		}
		// 代收费用
		String cost = "";
		if (mList.get(position).getNeedPayMoney().endsWith("00")) {
			cost = Integer.parseInt(mList.get(position).getNeedPayMoney()) / 100 + "元";
		} else {
			double result = MathUtils.div(mList.get(position).getNeedPayMoney(), 100, 1);
			cost = result + "";
		}
		viewHolder.mParcelNeedPayMoney.setText("代收费" + cost);
		return convertView;
	}

	private class ViewHolder {
		private ImageView mParcelOriginPic;
		private TextView mParcelOriginCompany;
		private TextView mParcelNumber;
		private TextView mParcelArriveShopTime;
		private TextView mParcelNeedPayMoney;
	}

}
